package com.weitao.marketserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weitao.marketserver.mapper.UserMap;
import com.weitao.marketserver.pojo.ReturnData;
import com.weitao.marketserver.pojo.User;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserCon {

    @Autowired
    private UserMap userMap;

    /**
     * 按页查找用户
     *
     * @param napage
     * @param size
     * @return
     */
    @RequestMapping("/selectbypage")
    public ReturnData getAll(@RequestParam(defaultValue = "") String phone,
                             @RequestParam(defaultValue = "10") Integer flgzhuangtai,
                             @RequestParam(value = "page", defaultValue = "1") Integer napage,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        ReturnData returnData = new ReturnData();

        Page<User> page = new Page<>(napage, size);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        if (!phone.equals("")) {
            queryWrapper.like("phone", phone);
        }

        if (!(flgzhuangtai == 10)) {
            queryWrapper.eq("flgzhuangtai", flgzhuangtai);
        }

        SelectWithParamResult s = new SelectWithParamResult();
        s.setList(userMap.selectPage(page, queryWrapper).getRecords());
        s.setCount(userMap.selectCount(queryWrapper));

        returnData.setDatas(s);
        return returnData;
    }

    @Data
    class SelectWithParamResult {
        List<User> list;
        int count;
    }

    /**
     * 用户总数
     *
     * @return
     */
    @RequestMapping("/usercount")
    public ReturnData getUserCount() {
        ReturnData returnData = new ReturnData();
        int count = userMap.selectCount(null);

        returnData.setDatas(count);
        return returnData;
    }

    /**
     * 根据ID修改用户
     *
     * @return
     */
    @PostMapping("/edituserbyid")
    public ReturnData editUserById(@RequestParam int id,
                                   @RequestParam String password,
                                   @RequestParam(defaultValue = "") String tixianzhanghu,
                                   @RequestParam int flgzhuangtai,
                                   @RequestParam BigDecimal zhanghujiner) {
        ReturnData returnData = new ReturnData();

        User u = userMap.selectById(id);
        u.setPassword(password);
        u.setTixianzhanghu(tixianzhanghu);
        u.setZhanghujiner(zhanghujiner);
        u.setFlgzhuangtai(flgzhuangtai);
        userMap.updateById(u);

        return returnData;
    }

    @RequestMapping("/byid/{id}")
    public User hello(@PathVariable(name = "id") Long id) {

        return null;
    }

    @RequestMapping("/add")
    public int add() {
        return 1;
    }

}
